#include <iostream>
using namespace std;

typedef struct intNode
{
    int data;
    intNode *next;
    intNode *pre;
} intNode;

typedef struct int_Doublelink
{
    intNode *head;
    int length;
} IDL;

bool insertNode(IDL *L, int index, int data)
{
    intNode *newNode = new intNode();
    newNode->data = data;
    if (L->length == 0)
    {
        L->head = newNode;
        L->length++;
        return true;
    }
    index = index > L->length ? L->length : index;
    intNode *curNode = L->head;
    for (int i = 0; i < L->length; i++)
    {
        if (index == 0) // 插头
        {
            L->head = newNode;
            newNode->next = curNode;
            curNode->pre = newNode;
            L->length++;
            break;
        }
        else if (L->length == index) // 尾部
        {
          newNode->next = curNode->next;
            curNode->next = newNode;
            newNode->pre = curNode;
             L->length++;
            break;
        }else{
            newNode->next = curNode->next;
            curNode->next = newNode;
            newNode->pre = curNode;
            newNode->next->pre = newNode;
            L->length++;
            break;
        }

        curNode = curNode->next;
    }
    return true;
}

bool head_insert(IDL *L, int num)
{
    return insertNode(L, 0, num);
}

bool end_insert(IDL *L, int num)
{
    return insertNode(L, L->length, num);
}

int findx(IDL *L, int num){
    intNode *curNode = L->head;
    for (int i = 0; i < L->length; i++)
    {
        if (curNode->data == num) 
        {
            return i;
        }
        curNode = curNode->next;
    }
    return -1;
}

bool deltNode(IDL *L, int index)
{
    if (L->length < 1 || index + 1 > L->length)
        return false;
    intNode *curNode = L->head;

    for (int i = 0; i < L->length; i++)
    {
        if (index == 0) // 插头
        {
            curNode->next->pre = L->head->pre;
            L->head = curNode->next;
            L->length--;
            break;
        }
        if (i + 1 == index) // 非头
        {
            curNode->next = curNode->next->next;
            curNode->next->pre = curNode;
            L->length--;
            break;
        }
        curNode = curNode->next;
    }
    return true;
}

void listPrint(IDL *L){
    if (L->length = 0) return;   
    intNode* curNode = L->head;
    do{
        printf("%d ",curNode->data);    
    }while(curNode = curNode->next);
    puts("");
}

IDL *init()
{
    IDL *L = new IDL();
    insertNode(L, 6, 1);
    insertNode(L, 6, 3);
    return L;
}

int main(int argc, char const *argv[])
{
    IDL* lIDL = init();
    insertNode(lIDL, 6, 2);
    printf("%d\n", lIDL->length);
    deltNode(lIDL, 0);
    printf("%d\n", lIDL->length);
    insertNode(lIDL, 0, 12);
    listPrint(lIDL);
    return 0;
}
